
#include "common.h"

unsigned int hz;

/*time_t get_localtime(struct tm *rectime)
{
    time_t timer;
    struct tm *ltm;
    
    time(&timer);
    ltm = localtime(&timer);
    
    *rectime = *ltm;
    return timer;
}*/

int get_HZ()
{
    long ticks;
    if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
        perror("sysconf");
    }
    hz = (int)ticks;
    return hz;
}

/*
 ***************************************************************************
 * Compute time interval.
 *
 * IN:
 * @prev_uptime Previous uptime value in jiffies.
 * @curr_uptime Current uptime value in jiffies.
 *
 * RETURNS:
 * Interval of time in jiffies.
 ***************************************************************************
 */
inline unsigned long long get_interval(unsigned long long prev_uptime,
                unsigned long long curr_uptime)
{
    unsigned long long itv;

    /* prev_time=0 when displaying stats since system startup */
    itv = curr_uptime - prev_uptime;

    if (!itv) { /* Paranoia checking */
        itv = 1;
    }

    return itv;
}

void swap_long(long *a, long *b)
{
    long c = *a;
    *a = *b;
    *b = c;
}

void swap_ll(long long *a, long long *b)
{
    long long c = *a;
    *a = *b;
    *b = c;
}

void swap_ptr(void **a, void **b)
{
    void *c = *a;
    *a = *b;
    *b = c;
}

void swap_int(int *a, int *b)
{
    int c = *a;
    *a = *b;
    *b = c;
}

void make_filename(char *buf)
{
    time_t rawtime;
    struct tm *timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    snprintf(buf, BUFFERSIZE, "%d_%d_%d_%d_%d_%d", timeinfo->tm_year+1900,
                                                   timeinfo->tm_mon+1,
                                                   timeinfo->tm_mday,
                                                   timeinfo->tm_hour,
                                                   timeinfo->tm_min,
                                                   timeinfo->tm_sec);
}
